// pages/shopingmall/order.js
const _request = require("../../utils/request.js");
const app = getApp()
Page({
  data: {
    mallimgUrl: app.globalData.mallimgUrl,
    shopimgUrl: app.globalData.shopimgUrl,
    cartId:'',//订单id
    defaultAddress: {},//选中地址
    remark: '',//备注
    goodsInfo: {},//订单详情
    total:0,//总商品数
    cardinfo:'',//选中的券信息
  },
  onLoad: function (options) {
    this.setData({
      cartId:options.cartId,
    })
    this.loaddata()//获取订单详情
    this.query()//获取地址
  },
  onShow: function () {
    console.log(this.data.cardinfo)
    let address = wx.getStorageSync('_shopmalladdress')
    if (address) {
      address = JSON.parse(address)
      this.setData({
        defaultAddress: address
      })
    }
  },
  //获取订单详情
  loaddata() {
    let dataJson = {
      userId: wx.getStorageSync('userId'),
      cartId:''
    }
    if(this.data.cartId!='null'){
      dataJson.cartId = this.data.cartId
    }
    _request.get('/shoppingTrolley/checkout', dataJson, app.globalData.mallType).then((res) => {
      if (res.data.code === 200) {
        let goodsInfo = res.data.data
        var _num = 0
        goodsInfo.checkedGoodsList.forEach(e => {
          _num = e.quantity + _num
          e.spObj = JSON.parse(e.sp1);
          let list = []
          e.spObj.forEach(item => {
            list.push(item.key + ":" + item.value)
          })
          e.spType =  list.join(',')
        })
        this.setData({
          total:_num,
          goodsInfo: goodsInfo,
        })
      }
    })
  },
  // 获取地址
  query() {
    _request.get('/receiveAndDeliver/getReceiveAndDeliverAddressByUserId', {
      id: wx.getStorageSync('userId')
    }, app.globalData.mallType).then((res) => {
      if (res.data.code === 200) {
        let list = res.data.data.rows || []
        let defaultAddress = list.filter(e => e.receiveStatus == 1)
        if (defaultAddress && defaultAddress.length) {
          this.setData({
            addressList: defaultAddress[0],
            defaultAddress:defaultAddress[0]
          })
          wx.setStorageSync('_shopmalladdress', JSON.stringify(defaultAddress[0]))
        }
      }
    })
  },
  // 跳转地址列表
  gotoSelectAddress() {
    wx.navigateTo({
      url: '/pages/shopingmall/addressmanager',
    })
  },
  // 跳转优惠券
  selectCoupon() {
    wx.navigateTo({
      url: '/pages/shopingmall/couponindex?cardinfo='+JSON.stringify(this.data.cardinfo),
    })
  },
  // 提交订单跳转
  handlepay() {
    if(!this.data.defaultAddress.id){
      wx.showToast({
        title: '请先选择地址',
        icon:'none'
      })
      return;
    }
    _request.postObj('/order/submitOrder', {
      userId: wx.getStorageSync('userId'),
      cartItemId:this.data.cartId,
      addressId:this.data.defaultAddress.id,
      note:this.data.remark,
      memberId:1,//会员id 后端说写死不知道哪来的
      couponId:this.data.cardinfo.id,//现金券id
      useIntegration:'',//使用的积分
    }, app.globalData.mallType).then((res) => {
      if (res.data.code === 200) {
        wx.navigateTo({
          url: '/pages/shopingmall/payment?orderId='+res.data.data.orderId+'&orderSn='+res.data.data.orderSn+'&realityPayPrice='+res.data.data.realityPayPrice+'&createordertime='+res.data.data.createordertime,
        })
      }else{
        wx.showToast({
          title: res.data.message,
          icon:'none'
        })
      }
    })
  },
  // 备注
  getremark(e){
    this.setData({
      remark:e.detail.value
    })
  },
})